﻿using System;
using System.Collections;
using System.Data;
using CE.Database;


/// <summary>
/// Summary description for TAAreaCountryDB
/// </summary>
/// 
namespace CE.Provider
{
    public class TAAreaCountryDB
    {
        public TAAreaCountryDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(TAAreaCountry TAAreaCountry)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderTAAreaCountryInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter

            _sPara.Add("@AcId", TAAreaCountry.AreaCountryId);
            _sPara.Add("@ProviderCompanyId", TAAreaCountry.ProviderCompanyId);
            _sPara.Add("@AreaId", TAAreaCountry.AreaId);
            _sPara.Add("@CountryId", TAAreaCountry.CountryId);
            _sPara.Add("@TAInsId", TAAreaCountry.TaInsId);
            _sPara.Add("@PlanType", TAAreaCountry.PlanType);
            _sPara.Add("@CoverageLevel", TAAreaCountry.CoverageLevel);
            _sPara.Add("@RefCEUserId", TAAreaCountry.RefCEUserId);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public TAAreaCountry GetTAAreaCountry(Int64 ArId, Int64 TaId, string PlanType)
        {
            TAAreaCountry _TAAreaCountry = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderTAAreaCountrySelectItemList]";

            SortedList _spara = new SortedList();
            _spara.Add("@AreaID", ArId);
            _spara.Add("@TAInsID", TaId);
            _spara.Add("@PlanType", PlanType);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _TAAreaCountry = new TAAreaCountry();
                    _TAAreaCountry = FillDataRecord(_dt, 0);
                }
            }
            return _TAAreaCountry;
        }

        public DataTable GetTAAreaCountryList(Int64 ArId, Int64 TaId, string PlanType)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("AreaCountries");
            string _spName = "[sprocProviderTAAreaCountrySelectItemList]";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@AreaID", ArId);
            _sPara.Add("@TAInsID", TaId);
            _sPara.Add("@PlanType", PlanType);

            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        private TAAreaCountry FillDataRecord(DataTable myDataTable, int rowId)
        {
            TAAreaCountry myTAAreaCountry = new TAAreaCountry();
            myTAAreaCountry.AreaCountryId = Convert.ToInt64(myDataTable.Rows[rowId]["AreaCountryId"].ToString());
            myTAAreaCountry.AreaId = Convert.ToInt64(myDataTable.Rows[rowId]["AreaId"].ToString());
            myTAAreaCountry.CountryId = Convert.ToInt64(myDataTable.Rows[rowId]["CountryId"].ToString());

            return myTAAreaCountry;
        }

        public Int64 GetAreaIDByCountryID(Int64 CoId)
        {
            Int64 AreaID = 0;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderTAAreaCountrySelectSingleItemByCountry]";

            SortedList _spara = new SortedList();
            _spara.Add("@CountryID", CoId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    AreaID = Convert.ToInt64(_dt.Rows[0]["AreaId"].ToString());
                }
            }
            return AreaID;


        }

        public void CheckAreaCountryItem(Int64 ArId, Int64 Taid, string PlanType)
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderTAAreaCountryCheckItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@AreaId", ArId);
            _spara.Add("@TAInsId", Taid);
            _spara.Add("@PlanType", PlanType);

            _db.ExecuteSP(_spName, _spara);

        }
    }
}
